﻿using Sustainalytics.ESG.Entities;
using Sustainalytics.Synchronization.Validation.ValidationRules.Comprehensive;
using Xunit;

namespace Sustainalytics.Synchronization.Validation.Tests
{
    [Trait("Category", "Unit")]
    public class LinkedIndicatorsRule_should
    {
        private readonly LinkedIndicatorsRule rule = new LinkedIndicatorsRule();
        private readonly RPCompanyESGComputationResult reference = new RPCompanyESGComputationResult();

        private RPIndicatorPerformance[] GetIndicators(string keysList, string valuesList)
        {
            var keys = keysList.Split(',');
            var values = valuesList.Split(',');

            var result = new RPIndicatorPerformance[keys.Length];

            for (var i = 0; i < keys.Length; i++)
                result[i] = new RPIndicatorPerformance() { Code = keys[i], RawScore = float.Parse(values[i]) };

            return result;
        }

        [Fact]
        public void PassRule_When_G21_notFound()
        {
            var validated = new RPCompanyESGComputationResult()
            {
                Indicators = GetIndicators("G.2.3,G.2.2", "0,0")
            };

            var result = rule.IsValid(validated, reference);

            Assert.True(result);
        }

        [Fact]
        public void PassRule_When_G21_isNotZero()
        {
            var validated = new RPCompanyESGComputationResult()
            {
                Indicators = GetIndicators("G.2.1,G.2.2", "1,0")
            };

            var result = rule.IsValid(validated, reference);

            Assert.True(result);
        }

        [Fact]
        public void PassRule_When_G21_isZero_and_G22_notFound()
        {
            var validated = new RPCompanyESGComputationResult()
            {
                Indicators = GetIndicators("G.2.1,G.2.3", "0,0")
            };

            var result = rule.IsValid(validated, reference);

            Assert.True(result);
        }

        [Fact]
        public void PassRule_When_G21_isZero_and_G22_isZero()
        {
            var validated = new RPCompanyESGComputationResult()
            {
                Indicators = GetIndicators("G.2.1,G.2.2", "0,0")
            };

            var result = rule.IsValid(validated, reference);

            Assert.True(result);
        }

        [Fact]
        public void BreakRule_When_G21_isZero_and_G22_isNotZero()
        {
            var validated = new RPCompanyESGComputationResult()
            {
                Indicators = GetIndicators("G.2.1,G.2.2", "0,1")
            };

            var result = rule.IsValid(validated, reference);

            Assert.False(result);
        }
    }
}
